package com.ibm.servlets;

import com.ibm.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/OrderDeleteServlet")
public class OrderDeleteServlet extends HttpServlet {
    private OrderService orderService=new OrderService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    @Override
    protected  void doPost(HttpServletRequest request,HttpServletResponse response)
        throws ServletException,IOException{
        //1.接收要删除订单的编号
        int orderId=Integer.parseInt(request.getParameter("orderId"));
        //2.删除订单
        boolean b=orderService.DeleteOrder(orderId);
        //3.响应ajax请求（响应json格式数据）
        String jsonStr = b?"{\"code\":1000,\"msg\":\"success\"}"
                :"{\"code\":1001,\"msg\":\"fail\"}";
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.println(jsonStr);
        out.flush();
        out.close();

    }

}
